package org.jeecg.modules.storage.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.storage.entity.BussWarehouseInventory;
import org.jeecg.modules.storage.entity.BussWarehouseInventoryDetail;
import org.jeecg.modules.storage.mapper.BussWarehouseInventoryDetailMapper;
import org.jeecg.modules.storage.mapper.BussWarehouseInventoryMapper;
import org.jeecg.modules.storage.service.IBussWarehouseInventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 盘点单
 * @Author: jeecg-boot
 * @Date:   2024-08-09
 * @Version: V1.0
 */
@Service
public class BussWarehouseInventoryServiceImpl extends ServiceImpl<BussWarehouseInventoryMapper, BussWarehouseInventory> implements IBussWarehouseInventoryService {

	@Autowired
	private BussWarehouseInventoryMapper bussWarehouseInventoryMapper;
	@Autowired
	private BussWarehouseInventoryDetailMapper bussWarehouseInventoryDetailMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(BussWarehouseInventory bussWarehouseInventory, List<BussWarehouseInventoryDetail> bussWarehouseInventoryDetailList) {
		bussWarehouseInventoryMapper.insert(bussWarehouseInventory);
		if(bussWarehouseInventoryDetailList!=null && bussWarehouseInventoryDetailList.size()>0) {
			for(BussWarehouseInventoryDetail entity:bussWarehouseInventoryDetailList) {
				//外键设置
				entity.setMainId(bussWarehouseInventory.getId());
			}
			bussWarehouseInventoryDetailMapper.insertBatchSomeColumn(bussWarehouseInventoryDetailList);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(BussWarehouseInventory bussWarehouseInventory,List<BussWarehouseInventoryDetail> bussWarehouseInventoryDetailList) {
		bussWarehouseInventoryMapper.updateById(bussWarehouseInventory);
		
		//1.先删除子表数据
		bussWarehouseInventoryDetailMapper.deleteByMainId(bussWarehouseInventory.getId());
		
		//2.子表数据重新插入
		if(bussWarehouseInventoryDetailList!=null && bussWarehouseInventoryDetailList.size()>0) {
			for(BussWarehouseInventoryDetail entity:bussWarehouseInventoryDetailList) {
				//外键设置
				entity.setMainId(bussWarehouseInventory.getId());

			}
			bussWarehouseInventoryDetailMapper.insertBatchSomeColumn(bussWarehouseInventoryDetailList);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		bussWarehouseInventoryDetailMapper.deleteByMainId(id);
		bussWarehouseInventoryMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			bussWarehouseInventoryDetailMapper.deleteByMainId(id.toString());
			bussWarehouseInventoryMapper.deleteById(id);
		}
	}
	
}
